#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+1;
int a[6][N],sum[6][N];
const int M = 1e9+7;
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
	{
		cin>>a[1][i];
	}
	for(int i = 2;i<=5;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			(a[i][j] = a[i-1][j]*a[1][j])%M;
		}
	}
	for(int i = 1;i<=5;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			sum[i][j] = (sum[i][j-1]+a[i][j])%M;
		}
	}
	while(m--)
	{
		int l,r,k;
		cin>>l>>r>>k;
		cout<<(sum[k][r] - sum[k][l-1] + M)%M<<endl;
	}
	return 0;
}
